package cn.tiga.system.controller;

import cn.tiga.basic.annotation.PreAuthorize;
import cn.tiga.system.service.IMenuService;
import cn.tiga.system.domain.Menu;
import cn.tiga.system.query.MenuQuery;
import cn.tiga.basic.util.JsonResult;
import cn.tiga.basic.util.PageList;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "菜单新增修改",sn = "menu:put")
    public JsonResult addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "菜单删除",sn = "menu:delete")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            menuService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return JsonResult.me().setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "菜单根据id查询",sn = "menu:get")
    public Menu get(@PathVariable("id")Long id)
    {
        return menuService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "菜单查询全部",sn = "menu:get")
    public List<Menu> list(){

        return menuService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "菜单分页",sn = "menu:post")
    public PageList<Menu> json(@RequestBody MenuQuery query)
    {
        return menuService.pageQuery(query);
    }

    /**
     * 查询菜单树
     * @return
     */
    @GetMapping("/menuTree")
    @ApiOperation(value = "获取菜单树-无限极接口")
    public List<Menu> menuTree(){
        return menuService.menuTree();
    }
}
